﻿namespace KLF.Lib.Browser.DOM.Events
{
    #region Using Statements

    using KLF.Lib.Browser.DOM.View;
    using KLF.Lib.Browser.DOM.HTML;

    #endregion

    /// <summary>
    /// The FocusEvent interface provides specific contextual information 
    /// associated with Focus events.
    /// 
    /// To create an instance of the FocusEvent interface, use the 
    /// DocumentEvent.createEvent("FocusEvent") method call.
    /// </summary>
    public class FocusEvent : UIEvent
    {
        public FocusEvent(string _type)
            : base(_type)
        {
        }

        #region DOM Level 3 Events Compliance

        /// <summary>
        /// Initializes attributes of a FocusEvent object. This method has the
        /// same behavior as UIEvent.initUIEvent(). 
        /// </summary>
        /// <param name="typeArg"></param>
        /// <param name="canBubbleEvent"></param>
        /// <param name="cancelableArg"></param>
        /// <param name="viewArg"></param>
        /// <param name="detailArg"></param>
        /// <param name="relatedTargetArg"></param>
        public void initFocusEvent(
            string typeArg,
            bool canBubbleEvent,
            bool cancelableArg,
            IAbstractView viewArg,
            int detailArg,
            IEventTarget relatedTargetArg)
        {
            base.initUIEvent(typeArg, canBubbleEvent, cancelableArg, viewArg, detailArg);

            switch (typeArg.ToLowerInvariant())
            {
                case HTMLConstants.HTML_EVENT_BLUR:
                case HTMLConstants.HTML_EVENT_FOCUS:
                    bubbles = cancelable = false;
                    break;

                case HTMLConstants.HTML_EVENT_DOM_FOCUS_IN:
                case HTMLConstants.HTML_EVENT_DOM_FOCUS_OUT:
                case HTMLConstants.HTML_EVENT_FOCUS_IN:
                case HTMLConstants.HTML_EVENT_FOCUS_OUT:
                    bubbles = true;
                    cancelable = false;
                    break;
            }

            relatedTarget = relatedTargetArg;
        }

        /// <summary>
        /// Used to identify a secondary EventTarget related to a Focus event,
        /// depending on the type of event.
        /// 
        /// For security reasons with nested browsing contexts, when tabbing
        /// into or out of a nested context, the relevant EventTarget should
        /// be null.
        /// </summary>
        public IEventTarget relatedTarget { get; internal set; }

        #endregion
    }
}
